﻿Imports System.IO

Public Class Metas
    Dim dtSemanal As DataTable
    Dim dtMensual As DataTable
    Dim dtClientes As DataTable

    Dim editando As Boolean = False
    Dim edit_fila As Boolean = False

    Dim colr As Color = Color.LightSteelBlue
    Dim colSel As String = "9,10,12"

    Private Sub Metas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not File.Exists(ruta_programa & "\Info.xml") Then
            msg.Mostrar("No se puede encontrar el archivo de Datos", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Carga_Datos()
        Carga_Metas()

        dsInfo = New DataSet
        dsInfo.ReadXml(ruta_programa & "\Info.xml")
        'dsInfo.Tables("CLIENTES").Columns("SALDO").ExtendedProperties.Add("formato", "$ ###,###,##0.00")
        'dsInfo.Tables("CUENTAS").Columns("SALDO").ExtendedProperties.Add("formato", "$ ###,###,##0.00")
        'dsInfo.Tables("CUENTAS").Columns("IMPORTE").ExtendedProperties.Add("formato", "$ ###,###,##0.00")


        lbPromotor.Text = CType(dsConfig.Tables("EMPRESA").Select("Concepto='Nombre del Vendedor'").GetValue(0), DataRow)(1)
        lbID.Text = CType(dsConfig.Tables("EMPRESA").Select("Concepto='ID del Vendedor'").GetValue(0), DataRow)(1)
        lbFecha.Text = Format(Now, "MMMM dd,yyyy")
        lbRuta.Text = Format(Now, "dddd").ToUpper


        Carga_Semanal()
        Carga_Mes()
        Carga_Clientes()

        dgDatos.DefaultCellStyle.SelectionBackColor = Color.LightSteelBlue

    End Sub
    Private Sub Metas_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.F1
                btnClientes_Click(Nothing, Nothing)

            Case Keys.F9
                btnEditar_Click(Nothing, Nothing)

            Case Keys.F10
                btnAltaClie_Click(Nothing, Nothing)
        End Select
    End Sub

    Private Sub Carga_Semanal()
        dtSemanal = New DataTable
        dtSemanal.Columns.Add("-4 Sem", GetType(Double)) '0
        dtSemanal.Columns.Add("-3 Sem", GetType(Double)) '1
        dtSemanal.Columns.Add("-2 Sem", GetType(Double)) '2
        dtSemanal.Columns.Add("-1 Sem", GetType(Double)) '3
        dtSemanal.Columns.Add("Actual", GetType(Double)) '4
        dtSemanal.Columns.Add("Objetivo del dia", GetType(Double)) '5

        dtSemanal.Rows.Clear()

        dgSemanal.DataSource = dtSemanal

        dgSemanal.Refresh()

        dgSemanal.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgSemanal.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgSemanal.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgSemanal.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgSemanal.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgSemanal.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        dgSemanal.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        dtSemanal.Columns(0).DefaultValue = 0
        dtSemanal.Columns(1).DefaultValue = 0
        dtSemanal.Columns(2).DefaultValue = 0
        dtSemanal.Columns(3).DefaultValue = 0
        dtSemanal.Columns(4).DefaultValue = 0
        dtSemanal.Columns(5).DefaultValue = 0

        dgSemanal.Columns(0).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgSemanal.Columns(1).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgSemanal.Columns(2).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgSemanal.Columns(3).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgSemanal.Columns(4).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgSemanal.Columns(5).DefaultCellStyle.Format = "$ ###,###,##0.00"

        Dim dia_semana As Integer = Now.DayOfWeek

        Dim sA As Date = DateAdd(DateInterval.Day, (dia_semana - 1) * -1, Now)
        Dim s4 As Date = DateAdd(DateInterval.Day, -7, sA)
        Dim s3 As Date = DateAdd(DateInterval.Day, -7, s4)
        Dim s2 As Date = DateAdd(DateInterval.Day, -7, s3)
        Dim s1 As Date = DateAdd(DateInterval.Day, -7, s2)

        Dim fila As DataRow = dtSemanal.NewRow

        fila(0) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s1, "dd/MM/yyyy") & "' AND FECHA<'" & Format(s2, "dd/MM/yyyy") & "'")
        fila(1) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s2, "dd/MM/yyyy") & "' AND FECHA<'" & Format(s3, "dd/MM/yyyy") & "'")
        fila(2) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s3, "dd/MM/yyyy") & "' AND FECHA<'" & Format(s4, "dd/MM/yyyy") & "'")
        fila(3) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s4, "dd/MM/yyyy") & "' AND FECHA<'" & Format(sA, "dd/MM/yyyy") & "'")
        fila(4) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(sA, "dd/MM/yyyy") & "'")
        fila(5) = dsMetas.Tables("OBJETIVO").Compute("SUM(OBJETIVO)", "FECHA='" & Format(Now, "MMMM dd,yyyy") & "'")

        For i As Integer = 0 To 4
            If fila(i) Is DBNull.Value Then fila(i) = 0
        Next

        dtSemanal.Rows.Add(fila)


    End Sub

    Private Sub Carga_Mes()
        dtMensual = New DataTable
        dtMensual.Columns.Add("Meta del Mes", GetType(Double))
        dtMensual.Columns.Add("Venta Alcanzada", GetType(Double))
        dtMensual.Columns.Add("% Avance", GetType(Double))

        dtMensual.Rows.Clear()

        Dim fila As DataRow = dtMensual.NewRow

        dtMensual.Rows.Add(fila)

        dgMensual.DataSource = dtMensual
        dgMensual.Refresh()

        dgMensual.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgMensual.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dgMensual.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        dgMensual.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

        dgMensual.Columns(0).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgMensual.Columns(1).DefaultCellStyle.Format = "$ ###,###,##0.00"
        dgMensual.Columns(2).DefaultCellStyle.Format = "0%"

        Dim dia_mes As Integer = Now.Day
        Dim dIni As Date = DateAdd(DateInterval.Day, (dia_mes - 1) * -1, Now)

        fila(0) = dsMetas.Tables("META").Compute("SUM(META)", "MES='" & Format(Now, "MM") & "' AND AÑO='" & Format(Now, "yyyy") & "' AND VENDEDOR='" & lbPromotor.Text & "'")
        fila(1) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(dIni, "dd/MM/yyyy") & "'")
        fila(2) = 0

        If fila(0) Is DBNull.Value And fila(1) Is DBNull.Value Then
            fila(0) = 0
            fila(1) = 0
        End If
        If fila(0) Is DBNull.Value And fila(1) IsNot DBNull.Value Then
            fila(0) = 0
            fila(2) = 0 'fila(1) / 1
        End If
        If fila(0) IsNot DBNull.Value And fila(1) Is DBNull.Value Then
            fila(1) = 0
        End If
        If fila(0) > 0 And fila(1) > 0 Then
            fila(2) = fila(1) / fila(0)
        End If

    End Sub

    Private Sub Carga_Clientes()

        dtClientes = New DataTable
        dtClientes.Columns.Add("ID", GetType(String)) '0
        dtClientes.Columns.Add("Nombre del Cliente", GetType(String)) '1
        dtClientes.Columns.Add("Venta Referencia", GetType(Double)) '2
        dtClientes.Columns.Add("Ultimo Mes", GetType(Double)) '3
        dtClientes.Columns.Add("-4 sem", GetType(Double)) '4
        dtClientes.Columns.Add("-3 sem", GetType(Double)) '5
        dtClientes.Columns.Add("-2 sem", GetType(Double)) '6
        dtClientes.Columns.Add("-1 sem", GetType(Double)) '7
        dtClientes.Columns.Add("Promedio Semanal", GetType(Double)) ' 8
        dtClientes.Columns.Add("Objetivo", GetType(Double)) '9
        dtClientes.Columns.Add("Estrategia", GetType(String)) '10
        dtClientes.Columns.Add("Pedido", GetType(Double)) '11
        dtClientes.Columns.Add("Notas", GetType(String)) '12
        dtClientes.Columns.Add("_", GetType(Image)) '13

        '********************************************************************************************************************
        '       Carga de las semanas y del mes por cliente
        '********************************************************************************************************************
        Dim dia_semana As Integer = Now.DayOfWeek
        Dim dia_mes As Integer = Now.Day

        Dim dIni As Date = DateAdd(DateInterval.Day, (dia_mes - 1) * -1, Now)
        Dim sA As Date = DateAdd(DateInterval.Day, (dia_semana - 1) * -1, Now)
        Dim s4 As Date = DateAdd(DateInterval.Day, -7, sA)
        Dim s3 As Date = DateAdd(DateInterval.Day, -7, s4)
        Dim s2 As Date = DateAdd(DateInterval.Day, -7, s3)
        Dim s1 As Date = DateAdd(DateInterval.Day, -7, s2)

        Dim cFila As DataRow

        For Each filac As DataRow In dsInfo.Tables("CLIENTES").Select("DIA='" & lbRuta.Text & "'")
            Dim cont As Integer = 0
            cFila = dtClientes.NewRow
            cFila(0) = filac("CCLIE")
            cFila(1) = filac("NOMBRE")
            cFila(2) = dsMetas.Tables("VENTAS").Compute("MAX(MONTO)", "CCLIE ='" & cFila(0) & "'")
            cFila(3) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(dIni, "dd/MM/yyyy") & "' AND CCLIE ='" & cFila(0) & "'")
            cFila(4) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s1, "dd/MM/yyyy") & "' AND FECHA<'" & Format(s2, "dd/MM/yyyy") & "' AND CCLIE ='" & cFila(0) & "'")
            cFila(5) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s2, "dd/MM/yyyy") & "' AND FECHA<'" & Format(s3, "dd/MM/yyyy") & "' AND CCLIE ='" & cFila(0) & "'")
            cFila(6) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s3, "dd/MM/yyyy") & "' AND FECHA<'" & Format(s4, "dd/MM/yyyy") & "' AND CCLIE ='" & cFila(0) & "'")
            cFila(7) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA>='" & Format(s4, "dd/MM/yyyy") & "' AND FECHA<'" & Format(sA, "dd/MM/yyyy") & "' AND CCLIE ='" & cFila(0) & "'")
            cFila(8) = 0

            '**************************************************
            '       Carga los datos de la tabla OBJETIVO
            '**************************************************
            Dim resultado As DataRow() = dsMetas.Tables("OBJETIVO").Select("FECHA='" & Format(Now, "MMMM dd,yyyy") & "' AND CCLIE ='" & cFila(0) & "'")

            If resultado.Length > 0 Then
                cFila(9) = resultado(0)("OBJETIVO")
                cFila(10) = resultado(0)("ESTRATEGIA")
                cFila(12) = resultado(0)("NOTAS")

            Else
                cFila(9) = 0
                cFila(10) = ""
                cFila(12) = ""
            End If

            For i As Integer = 2 To 7
                If cFila(i) Is DBNull.Value Then cFila(i) = 0
                If i > 3 Then cFila(8) += cFila(i)
            Next

            cFila(8) = cFila(8) / 4

            cFila(11) = dsMetas.Tables("VENTAS").Compute("SUM(MONTO)", "FECHA='" & Format(Now, "dd/MM/yyyy") & "' AND CCLIE ='" & cFila(0) & "'")
            If cFila(11) Is DBNull.Value Then cFila(11) = 0

            Carga_Fila(cFila)
            dtClientes.Rows.Add(cFila)
            cont = cont + 1
        Next

        dgDatos.DataSource = dtClientes
        dgDatos.Refresh()
        dgDatos.Visible = True

        For i As Integer = 2 To 9
            dgDatos.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            dtClientes.Columns(i).DefaultValue = 0
            dgDatos.Columns(i).DefaultCellStyle.Format = "$ ###,###,##0.00"
        Next

        dgDatos.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dtClientes.Columns(11).DefaultValue = 0
        dgDatos.Columns(11).DefaultCellStyle.Format = "$ ###,###,##0.00"

        dgDatos.DefaultCellStyle.SelectionBackColor = colr

    End Sub

    Private Sub Carga_Fila(ByRef fila As DataRow)

        Dim bmp As Bitmap

        If fila(11) > 0 Then
            bmp = New Bitmap(My.Resources.Banned, 30, 30)
        Else
            bmp = New Bitmap(My.Resources.arrow_right, 30, 30)
        End If


        fila(13) = bmp

    End Sub

    'dataset Metas
    Public Sub Carga_Metas()
        If Not File.Exists(ruta_programa & "\Metas.xml") Then 'Si no existe Crealo!
            dsMetas = New Data.DataSet

            Dim dtMetas As New DataTable("META")
            dtMetas.Columns.Add("VENDEDOR", GetType(String))
            dtMetas.Columns.Add("MES", GetType(String))
            dtMetas.Columns.Add("AÑO", GetType(String))
            dtMetas.Columns.Add("META", GetType(Double))

            Dim dtObjetivo As New DataTable("OBJETIVO")
            dtObjetivo.Columns.Add("CCLIE", GetType(String))
            dtObjetivo.Columns.Add("FECHA", GetType(String))
            dtObjetivo.Columns.Add("OBJETIVO", GetType(Double))
            dtObjetivo.Columns.Add("ESTRATEGIA", GetType(String))
            dtObjetivo.Columns.Add("NOTAS", GetType(String))


            Dim dtVentas As New DataTable("VENTAS")
            dtVentas.Columns.Add("CCLIE", GetType(String))
            dtVentas.Columns.Add("PEDIDO", GetType(String))
            dtVentas.Columns.Add("FECHA", GetType(Date))
            dtVentas.Columns.Add("MONTO", GetType(Double))

            dsMetas.Tables.Add(dtMetas)
            dsMetas.Tables.Add(dtObjetivo)
            dsMetas.Tables.Add(dtVentas)
            'Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            dsMetas.WriteXml(ruta_programa & "\Metas.xml", XmlWriteMode.WriteSchema)
        End If

        dsMetas = New Data.DataSet
        dsMetas.ReadXml(ruta_programa & "\Metas.xml")
    End Sub

    Private Sub dgDatos_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgDatos.CellContentClick


        If e.ColumnIndex = 13 AndAlso dgDatos.Rows(e.RowIndex).Cells(11).Value = 0 Then
            Dim id As String = dgDatos.Rows(e.RowIndex).Cells(0).Value

            Dim regreso As Boolean

            Dim forma As New Pedidos(id, regreso)
            If regreso Then
                Me.Hide()
                forma.ShowDialog()
                forma.Dispose()
                Me.Show()
                Me.BringToFront()

                Carga_Semanal()
                Carga_Mes()
                Carga_Clientes()
            Else
                forma.Dispose()
            End If

        End If
    End Sub

    Private Sub btnClientes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClientes.Click
        Dim forma As New Pedidos()
        Me.Hide()
        forma.ShowDialog()
        forma.Dispose()
        Me.Show()
        Me.BringToFront()

        Carga_Semanal()
        Carga_Mes()
        Carga_Clientes()
    End Sub

    Private Sub btnAltaClie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAltaClie.Click
        Dim forma As New Alta_Clientes()
        Me.Hide()
        forma.ShowDialog()
        forma.Dispose()
        Me.Show()
        Me.BringToFront()

        Carga_Semanal()
        Carga_Mes()
        Carga_Clientes()
    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click

        If editando = False Then

            btnEditar.Text = "Guardar Metas (F9)"

            editando = True

            dgMensual.ReadOnly = False

            dgMensual.Rows(0).Cells(0).ReadOnly = False
            dgMensual.Rows(0).Cells(1).ReadOnly = True
            dgMensual.Rows(0).Cells(2).ReadOnly = True


            dgMensual.Rows(0).Cells(0).Style.BackColor = colr
            dgMensual.DefaultCellStyle.SelectionBackColor = colr

            dgMensual.SelectionMode = DataGridViewSelectionMode.CellSelect

            '-------------------------------------------------------
            dgDatos.ReadOnly = False
            dgDatos.SelectionMode = DataGridViewSelectionMode.CellSelect

            Dim seleccionado As Boolean = False


            For Each fila As DataGridViewRow In dgDatos.Rows
                For Each celda As DataGridViewCell In fila.Cells
                    If colSel.Split(",").Contains(celda.ColumnIndex.ToString) Then
                        celda.ReadOnly = False
                        celda.Style.BackColor = colr
                        If Not seleccionado Then celda.Selected = True
                        'celda.Selected = True
                    Else
                        celda.ReadOnly = True
                        celda.Style.BackColor = Color.White
                        'celda.Selected = True
                    End If
                Next
            Next

            dgDatos.Refresh()

            dgMensual.Rows(0).Cells(0).Selected = True
            dgMensual.Refresh()
            dgMensual.Focus()
            dgMensual.Select()
            dgMensual.Enabled = True
            Me.Refresh()

            For Each celda As DataGridViewCell In dgDatos.SelectedCells
                celda.Selected = False
            Next

            If dgDatos.Rows.Count > 0 Then dgDatos.FirstDisplayedCell = dgDatos.Rows(0).Cells(0)


        Else
            editando = False

            btnEditar.Text = "Editar Metas (F9)"

            Dim fila_ob, fila_meta As DataRow

            For Each row_meta As DataGridViewRow In dgMensual.Rows
                If row_meta.Cells(0).Value > 0 Then
                    If dsMetas.Tables("META").Rows.Count = 0 Then
                        fila_meta = dsMetas.Tables("META").NewRow
                        fila_meta("VENDEDOR") = lbPromotor.Text
                        fila_meta("MES") = Format(Now, "MM")
                        fila_meta("AÑO") = Format(Now, "yyyy")
                        fila_meta("META") = row_meta.Cells(0).Value
                        dsMetas.Tables("META").Rows.Add(fila_meta)
                    Else

                        Dim cambio As Boolean = False

                        For Each linea As DataRow In dsMetas.Tables("META").Rows
                            If linea(0) = lbPromotor.Text And linea(1) = Format(Now, "MM") And linea(2) = Format(Now, "yyyy") Then
                                linea(3) = row_meta.Cells(0).Value
                                cambio = True
                            End If
                        Next
                        If cambio = False Then
                            For Each linea As DataRow In dsMetas.Tables("META").Rows
                                If linea(0) = lbPromotor.Text And linea(1) = Format(Now, "MM") And linea(2) <> Format(Now, "yyyy") Then
                                    fila_meta = dsMetas.Tables("META").NewRow
                                    fila_meta("VENDEDOR") = lbPromotor.Text
                                    fila_meta("MES") = Format(Now, "MM")
                                    fila_meta("AÑO") = Format(Now, "yyyy")
                                    fila_meta("META") = row_meta.Cells(0).Value
                                    dsMetas.Tables("META").Rows.Add(fila_meta)
                                    cambio = True
                                    Exit For
                                End If
                            Next
                        End If
                        If cambio = False Then
                            For Each linea As DataRow In dsMetas.Tables("META").Rows
                                If linea(0) = lbPromotor.Text And linea(1) <> Format(Now, "MM") And linea(2) = Format(Now, "yyyy") Then
                                    fila_meta = dsMetas.Tables("META").NewRow
                                    fila_meta("VENDEDOR") = lbPromotor.Text
                                    fila_meta("MES") = Format(Now, "MM")
                                    fila_meta("AÑO") = Format(Now, "yyyy")
                                    fila_meta("META") = row_meta.Cells(0).Value
                                    dsMetas.Tables("META").Rows.Add(fila_meta)
                                    cambio = True
                                    Exit For
                                End If
                            Next
                        End If
                        If cambio = False Then
                            For Each linea As DataRow In dsMetas.Tables("META").Rows
                                If linea(0) <> lbPromotor.Text And linea(1) <> Format(Now, "MM") And linea(2) <> Format(Now, "yyyy") Then
                                    fila_meta = dsMetas.Tables("META").NewRow
                                    fila_meta("VENDEDOR") = lbPromotor.Text
                                    fila_meta("MES") = Format(Now, "MM")
                                    fila_meta("AÑO") = Format(Now, "yyyy")
                                    fila_meta("META") = row_meta.Cells(0).Value
                                    dsMetas.Tables("META").Rows.Add(fila_meta)
                                    Exit For
                                End If
                            Next
                        End If
                    End If
                End If
            Next

            Dim i As Integer = 0

            Dim Verif As Boolean = False
            Dim igual As Boolean = False
            For Each fila As DataGridViewRow In dgDatos.Rows
                If fila.Cells(9).Value = 0 Then
                    i = i + 1
                    Verif = True
                Else
                    If dsMetas.Tables("Objetivo").Rows.Count = 0 Then
                        fila_ob = dsMetas.Tables("OBJETIVO").NewRow
                        fila_ob("CCLIE") = dgDatos.Rows.Item(i).Cells(0).Value
                        fila_ob("FECHA") = lbFecha.Text 'fila(Format(Now, "dd/MM/yyyy"))
                        fila_ob("OBJETIVO") = dgDatos.Rows.Item(i).Cells(9).Value
                        fila_ob("ESTRATEGIA") = dgDatos.Rows.Item(i).Cells(10).Value
                        fila_ob("NOTAS") = dgDatos.Rows.Item(i).Cells(12).Value
                        dsMetas.Tables("OBJETIVO").Rows.Add(fila_ob)
                    Else
                        For Each row As DataRow In dsMetas.Tables("Objetivo").Rows
                            If fila.Cells(0).Value = row(0) And row(1) = lbFecha.Text Then
                                row("OBJETIVO") = dgDatos.Rows.Item(i).Cells(9).Value
                                row("ESTRATEGIA") = dgDatos.Rows.Item(i).Cells(10).Value
                                row("NOTAS") = dgDatos.Rows.Item(i).Cells(12).Value
                                igual = True
                            End If

                        Next
                        If igual = False Then
                            fila_ob = dsMetas.Tables("OBJETIVO").NewRow
                            fila_ob("CCLIE") = dgDatos.Rows.Item(i).Cells(0).Value
                            fila_ob("FECHA") = lbFecha.Text 'fila(Format(Now, "dd/MM/yyyy"))
                            fila_ob("OBJETIVO") = dgDatos.Rows.Item(i).Cells(9).Value
                            fila_ob("ESTRATEGIA") = dgDatos.Rows.Item(i).Cells(10).Value
                            fila_ob("NOTAS") = dgDatos.Rows.Item(i).Cells(12).Value
                            dsMetas.Tables("OBJETIVO").Rows.Add(fila_ob)
                        End If
                        igual = False
                        i = i + 1
                    End If
                End If
            Next
            If Verif = True Then
                If msg.Mostrar("Existe al menos una fila en la que necesita ingresar los datos completos." & vbCrLf & vbCrLf & "Desea Continuar?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = "&No" Then
                    Verif = False
                    Exit Sub
                End If
            End If


            For Each fila As DataGridViewRow In dgDatos.Rows
                For Each celda As DataGridViewCell In fila.Cells
                    celda.ReadOnly = True
                    celda.Style.BackColor = Color.White
                Next
            Next

            dgMensual.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            dgDatos.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            dgDatos.ReadOnly = True


            dgMensual.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            dgMensual.DefaultCellStyle.SelectionBackColor = Color.White
            dgMensual.Rows(0).Cells(0).Selected = True
            dgMensual.Rows(0).Cells(0).Style.SelectionBackColor = Color.White
            dgMensual.Rows(0).Cells(0).Style.BackColor = Color.White
            dgMensual.ReadOnly = True

        End If
        Guarda_Datos()
        Carga_Mes()
        Carga_Semanal()
        'Carga_Clientes()
    End Sub

    Private Sub dgMensual_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgMensual.GotFocus
        For Each celda As DataGridViewCell In dgDatos.SelectedCells
            celda.Selected = False
        Next
    End Sub

    Private Sub dgMensual_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgMensual.SelectionChanged
        If dgMensual.SelectedCells.Count > 0 AndAlso dgMensual.Rows(0).Cells(0).Selected = True AndAlso editando Then
            dgMensual.DefaultCellStyle.SelectionBackColor = Color.SpringGreen
            dgMensual.DefaultCellStyle.SelectionForeColor = Color.White
        Else
            dgMensual.DefaultCellStyle.SelectionBackColor = Color.White
            dgMensual.DefaultCellStyle.SelectionForeColor = Color.Black
        End If
    End Sub

    Private Sub dgDatos_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles dgDatos.DataError
        If e.ColumnIndex = 9 Then
            msg.Mostrar("El dato debe de ser numerico", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
            dgDatos.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
        End If
    End Sub

    Private Sub dgDatos_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgDatos.GotFocus
        For Each celda As DataGridViewCell In dgMensual.SelectedCells
            celda.Selected = False

        Next
    End Sub

    Private Sub dgDatos_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgDatos.SelectionChanged
        If dgDatos.SelectedCells.Count > 0 AndAlso colSel.Split(",").Contains(dgDatos.SelectedCells(0).ColumnIndex.ToString) AndAlso editando Then
            dgDatos.DefaultCellStyle.SelectionBackColor = Color.SpringGreen
            dgDatos.DefaultCellStyle.SelectionForeColor = Color.White
        Else
            dgDatos.DefaultCellStyle.SelectionBackColor = Color.LightSteelBlue
            dgDatos.DefaultCellStyle.SelectionForeColor = Color.Black

            If dgDatos.SelectedRows.Count > 0 AndAlso dgDatos.Rows(dgDatos.SelectedRows(0).Index).Cells(12).Value IsNot DBNull.Value Then
                txtNotas.Text = dgDatos.Rows(dgDatos.SelectedRows(0).Index).Cells(12).Value
                txtEstrategia.Text = dgDatos.Rows(dgDatos.SelectedRows(0).Index).Cells(10).Value

            Else
                txtNotas.Text = ""
                txtEstrategia.Text = ""

            End If
        End If
    End Sub

End Class
